package com.zzj.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * @Description TODO
 * @Author zzj
 * @Date 2020/8/6 18:12
 */
@WebServlet("/order/*")
public class OrderServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取cookie，拿到cookie中的sessionid
        Cookie[] cookies = req.getCookies();
        // 遍历cookie
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                System.out.println(cookies[i].getName() + cookies[i].getValue());
            }
        }
        // 获取session
        HttpSession session = req.getSession();
        System.out.println(session.getId());

        // 校验用户是否登录成功
        // 1.获取session中的userInfo
        Object userInfo = session.getAttribute("userInfo");
        // 2.判断userInfo是否为空
        if (userInfo == null) {
            System.out.println("支付失败");
            req.getRequestDispatcher("/error.jsp").forward(req, resp);
        } else {
            System.out.println("支付成功");
            req.getRequestDispatcher("/success.jsp").forward(req, resp);

        }
    }
}
